//
//  nextDetailZhuanJiaViewController.m
//  YuErmuYing
//
//  Created by Tiancheng on 16/9/3.
//  Copyright © 2016年 Tiancheng. All rights reserved.
//

#import "nextDetailZhuanJiaViewController.h"
#import "ChildBaseViewController.h"
#import "zhuanJiaJiangTang.h"
#import "sendMessageViewController.h"
#import "HttpRequest.h"
#import "UIImageView+WebCache.h"
#import "Macros.h"
#import "UILabel+LabelHeightAndWidth.h"
#import "nextDetailTableViewCell.h"
#import "nextDetail.h"
#import "NSString+NSDateToNSString.h"
#import "answerQuestionViewController.h"

#import "SVProgressHUD.h"
@interface nextDetailZhuanJiaViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UIView *headerView;
@property (weak, nonatomic) IBOutlet UIView *headerView01;
@property(nonatomic,copy)NSMutableArray*json_Array;
//view
@property (weak, nonatomic) IBOutlet UIView *nextView;
//发帖人头像
@property (weak, nonatomic) IBOutlet UIImageView *headImageView;
//标题
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
//价格:价钱
@property (weak, nonatomic) IBOutlet UILabel *priceLabel;
//名字
@property (weak, nonatomic) IBOutlet UILabel *userNameLabel;
//图片
@property(nonatomic,retain)UIImageView *picImageView01;
@property(nonatomic,retain)UIImageView *picImageView02;
@property(nonatomic,retain)UIImageView *picImageView03;

@property(nonatomic,strong)UITableView *myTableView;
//问题内容
@property(nonatomic,retain)UILabel*questionLabel;
@property(nonatomic,retain)nextDetailTableViewCell *myCell;
//myTableView_y
@property(nonatomic,assign)float myTableView_y;
@end

@implementation nextDetailZhuanJiaViewController
@synthesize json_Array,myCell,picImageView01,picImageView02,picImageView03,myTableView_y;
-(void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden=YES;
    self.tabBarController.tabBar.hidden=YES;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.headerView.backgroundColor=[UIColor colorWithRed:244/255.0 green:143/255.0 blue:177/255.0 alpha:1.0];
    self.headerView01.backgroundColor=[UIColor colorWithRed:240/255.0 green:98/255.0 blue:146/255.0 alpha:1.0];
    json_Array=[NSMutableArray array];
    [self setNextView00];
    [self getDataFromNet];
    self.myTableView.hidden=YES;
    [SVProgressHUD showWithStatus:@"加载中请稍后..."];

}
-(void)setNextView00
{
    zhuanJiaJiangTang *zjjh=[zhuanJiaJiangTang shardeManager];
    NSURL* imagePath = [NSURL URLWithString:zjjh.user_pic];
    //给一张默认图片，先使用默认图片，当图片加载完成后再替换
    [self.headImageView sd_setImageWithURL:imagePath placeholderImage:[UIImage imageNamed:@"default"]];
    self.titleLabel.text=zjjh.note_name;
    self.userNameLabel.text=zjjh.username;
    CGRect rect = self.nextView.frame;
    CGFloat questionLabel_y=rect.size.height+rect.origin.y;
    self.questionLabel=[[UILabel alloc]initWithFrame:CGRectMake(5, questionLabel_y, screenWidth-10, 10)];
    self.questionLabel.text=zjjh.note_describle;
    self.priceLabel.textColor=pinkColor;
    self.priceLabel.text=[NSString stringWithFormat:@"¥  %@",zjjh.note_price];
//*********************************************************
    //自适应高度
    self.questionLabel.textColor=[UIColor grayColor];
    self.questionLabel.font=[UIFont systemFontOfSize:13.0];
    self.questionLabel.numberOfLines = 100;
    CGFloat height=[UILabel getHeightByWidth:self.questionLabel.frame.size.width title:self.questionLabel.text font:self.questionLabel.font];
    self.questionLabel.frame = CGRectMake(5,questionLabel_y+10, screenWidth-10, height);
    [self.view addSubview:self.questionLabel];
    CGFloat view00_y=self.questionLabel.frame.origin.y+self.questionLabel.frame.size.height;
    
    CGFloat picImageView_y=view00_y;
     //加三张图片
    self.picImageView01=[[UIImageView alloc]initWithFrame:CGRectMake(12, picImageView_y, 0.29867*screenWidth, 0.2*screenWidth)];
//    self.picImageView01.backgroundColor=[UIColor orangeColor];
    [self.view addSubview:self.picImageView01];
    self.picImageView02=[[UIImageView alloc]initWithFrame:CGRectMake(12+(0.29867*screenWidth+2), picImageView_y, 0.29867*screenWidth, 0.2*screenWidth)];
//    self.picImageView02.backgroundColor=[UIColor orangeColor];
    [self.view addSubview:self.picImageView02];
    self.picImageView03=[[UIImageView alloc]initWithFrame:CGRectMake(12+(0.29867*screenWidth+2)*2, picImageView_y, 0.29867*screenWidth, 0.2*screenWidth)];
//    self.picImageView03.backgroundColor=[UIColor orangeColor];
    [self.view addSubview:self.picImageView03];
    //tableview的创建

    //添加图片里的照片
    if ([zjjh.picArray isKindOfClass:[NSArray class]])
    {
        myTableView_y=self.picImageView01.frame.size.height+self.picImageView01.frame.origin.y+5;
        picImageView01.hidden=NO;
        picImageView02.hidden=NO;
        picImageView03.hidden=NO;
        if (zjjh.picArray.count==1)
        {
            NSString *urlString0=zjjh.picArray[0];
            NSURL* imagePath0 = [NSURL URLWithString: urlString0];
            //给一张默认图片，先使用默认图片，当图片加载完成后再替换
            [picImageView01 sd_setImageWithURL:imagePath0 placeholderImage:[UIImage imageNamed:@"default"]];
        }
        else if(zjjh.picArray.count==2)
        {
            NSString *urlString0=zjjh.picArray[0];
            NSString *urlString1=zjjh.picArray[1];
            NSURL* imagePath0 = [NSURL URLWithString: urlString0];
            //给一张默认图片，先使用默认图片，当图片加载完成后再替换
            [picImageView01 sd_setImageWithURL:imagePath0 placeholderImage:[UIImage imageNamed:@"default"]];
            NSURL* imagePath1 = [NSURL URLWithString: urlString1];
            //给一张默认图片，先使用默认图片，当图片加载完成后再替换
            [picImageView02 sd_setImageWithURL:imagePath1 placeholderImage:[UIImage imageNamed:@"default"]];
            
        }
        else if(zjjh.picArray.count==3)
        {
            NSString *urlString0=zjjh.picArray[0];
            NSString *urlString1=zjjh.picArray[1];
            NSString *urlString2=zjjh.picArray[2];
            NSURL* imagePath0 = [NSURL URLWithString: urlString0];
            //给一张默认图片，先使用默认图片，当图片加载完成后再替换
            [picImageView01 sd_setImageWithURL:imagePath0 placeholderImage:[UIImage imageNamed:@"default"]];
            NSURL* imagePath1 = [NSURL URLWithString: urlString1];
            //给一张默认图片，先使用默认图片，当图片加载完成后再替换
            [picImageView02 sd_setImageWithURL:imagePath1 placeholderImage:[UIImage imageNamed:@"default"]];
            NSURL* imagePath2 = [NSURL URLWithString: urlString2];
            //给一张默认图片，先使用默认图片，当图片加载完成后再替换
            [picImageView03 sd_setImageWithURL:imagePath2 placeholderImage:[UIImage imageNamed:@"default"]];
        }
    }
    else
    {
        picImageView01.hidden=YES;
        picImageView02.hidden=YES;
        picImageView03.hidden=YES;
        myTableView_y=self.questionLabel.frame.origin.y+self.questionLabel.frame.size.height;
    }
//    CGFloat myTableView_y=self.picImageView01.frame.size.height+self.picImageView01.frame.origin.y+5;
    self.myTableView=[[UITableView alloc]initWithFrame:CGRectMake(0, myTableView_y, screenWidth, screenHeight-myTableView_y) style:UITableViewStylePlain];
    self.myTableView.backgroundColor=[UIColor whiteColor];
    self.myTableView.delegate=self;
    self.myTableView.dataSource=self;
    [self.view addSubview:self.myTableView];
}
//从服务器获取数据
-(void)getDataFromNet
{
    NSString*urlString=[NSString stringWithFormat:@"http://112.74.114.1/Parenting/index.php/api/note/notedetail"];
    NSMutableDictionary *parameters=[NSMutableDictionary dictionary];
    parameters[@"note_id"]=@"1";
    HttpRequest *httpRequest=[HttpRequest sharedInstance];
    [httpRequest postWithURLString:urlString parameters:parameters success:^(id responseObject)
     {
         id json = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
         //获取数组
         json_Array=[json objectForKey:@"data"];
         if (json_Array.count>0)
         {
             [self dismiss];
         }
//         [self.myTableView reloadData];
//         NSLog(@"json_Array=%@",json_Array);
     } failure:^(NSError *error) {
         NSLog(@"error=%@",error);
     }];
}
- (void)dismiss
{
    self.myTableView.hidden=NO;
    [SVProgressHUD dismiss];
    [self.myTableView reloadData];

}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
    return json_Array.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    nextDetail *nd=[nextDetail shardeManager];
    zhuanJiaJiangTang *zjjt=[zhuanJiaJiangTang shardeManager];
    myCell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (myCell == nil)
    {
        myCell = [[[NSBundle mainBundle] loadNibNamed:@"nextDetailTableViewCell" owner:self options:nil]lastObject];
        //设置点击效果,点击后不会产生灰色
        myCell.selectionStyle=UITableViewCellSelectionStyleNone;
    }
    if (json_Array.count>0)
    {
        zjjt.username=[json_Array[indexPath.row] objectForKey:@"user_name"];
        zjjt.user_pic=[json_Array[indexPath.row] objectForKey:@"user_pic"];
        zjjt.note_id=[json_Array[indexPath.row] objectForKey:@"note_id"];
        nd.comment_body=[json_Array[indexPath.row] objectForKey:@"comment_body"];
        nd.comment_pubtime=[json_Array[indexPath.row] objectForKey:@"comment_pubtime"];
        NSString *timeString=[NSString StringNSDateToNSString:nd.comment_pubtime];
        myCell.userNameLabel.text=zjjt.username;
        myCell.contentLabel.text=nd.comment_body;
        myCell.timeLabel.text=timeString;
        //自适应高度
        myCell.contentLabel.font = [UIFont systemFontOfSize:13];
        myCell.contentLabel.numberOfLines = 100;
        CGFloat height=[UILabel getHeightByWidth:self.myCell.contentLabel.frame.size.width title:self.myCell.contentLabel.text font:self.myCell.contentLabel.font];
        myCell.contentLabel.frame = CGRectMake(5, self.myCell.contentLabel.frame.origin.y,myCell.contentLabel.frame.size.width, height);
        float time_y=myCell.contentLabel.frame.origin.y+myCell.contentLabel.frame.size.height+15;
        myCell.timeLabel.frame=CGRectMake(5, time_y,myCell.timeLabel.frame.size.width,myCell.timeLabel.frame.size.height);
        CGRect rect = myCell.frame;
        rect.size.height = myCell.contentLabel.frame.size.height+115-21;
        myCell.frame = rect;
    }
    NSURL* imagePath = [NSURL URLWithString:zjjt.user_pic];
    //给一张默认图片，先使用默认图片，当图片加载完成后再替换
    [myCell.userPic sd_setImageWithURL:imagePath placeholderImage:[UIImage imageNamed:@"default"]];
    return myCell;

}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    self.myTableView=[self tableView:tableView cellForRowAtIndexPath:indexPath];
    //    self.myCell=[self tableview];
    return self.myTableView.frame.size.height;
}
- (IBAction)goBack:(id)sender
{
    ChildBaseViewController *cbvc=self.navigationController.viewControllers[1];
    [self.navigationController popToViewController:cbvc animated:YES];
}
- (IBAction)nextView:(id)sender
{
    zhuanJiaJiangTang*zjjt=[zhuanJiaJiangTang shardeManager];
    zjjt.isFirstOrNot=2;
    answerQuestionViewController *smvc=[answerQuestionViewController new];
    [self.navigationController pushViewController:smvc animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
